package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.service.ISysUserService;
import edu.whut.mall.admin.shiro.util.ShiroUtil;
import edu.whut.mall.admin.vo.LoginVo;
import edu.whut.mall.common.api.CommonResult;
import edu.whut.mall.common.util.IpUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * @program: d9lab-mall
 * @description: 登陆控制器
 * @author: Wayne
 * @create: 2020-04-19 16:33
 **/
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/login")
@RestController
@Api(value="登陆管理器",tags = {"登陆管理器"})
public class PassPortController {

    private final ISysUserService sysUserService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonResult login(HttpServletRequest httpServletRequest,
                              @NotBlank @RequestParam String username,
                              @NotBlank @RequestParam String password){

        LoginVo loginVo = sysUserService.loginAdmin(IpUtil.getIpAddr(httpServletRequest),username,password);

        return CommonResult.success(loginVo,"登陆成功");
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public CommonResult logout(){
        ShiroUtil.getSubject().logout();
        return CommonResult.success("退出成功");
    }
}
